//
//  ViewController.swift
//  tree_ai
//
//  Created by oldmacbook on 2024/5/25.
//

import UIKit
import SnapKit
import Swifter


class ViewController: UITabBarController {
    private var server: HttpServer?
    private var testcount: Int64?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        //let rootViewController = UITabBarController()
        //startTestHttpserver()
        
        
        let firstViewController = APPHomeViewController()
                let firstNavController = UINavigationController(rootViewController: firstViewController)
                firstNavController.tabBarItem.title = "首页"
                firstNavController.tabBarItem.image = UIImage(systemName: "house.fill")
                
                // 创建第二个子控制器
                let secondViewController = RecordListViewController()
                let secondNavController = UINavigationController(rootViewController: secondViewController)
                secondNavController.tabBarItem.title = "我的"
                secondNavController.tabBarItem.image = UIImage(systemName: "person.fill")
                
                // 将子控制器添加到UITabBarController
        self.viewControllers = [firstNavController, secondNavController]

                // 3. 将根视图控制器设置为导航控制器的根视图控制器
        //self.viewControllers = [rootViewController]
    }
    
    
    
    func startTestHttpserver() {
        self.server = HttpServer()
        
        
        self.server?["/hello"] = { .ok(.htmlBody("You asked for \($0) testcount:\(self.testcount)"))  }
        testcount?+=1
        
        let semaphore = DispatchSemaphore(value: 0)
        do {
          try self.server?.start(9080, forceIPv4: true)
          print("Server has started ( port = \(try self.server?.port()) ). Try to connect now...")
          //semaphore.wait()
        } catch {
          print("Server start error: \(error)")
          //semaphore.signal()
        }
    }

}

